package org.wanghong.gift.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.wanghong.gift.client.SmsValidateCodeClient;
import org.wanghong.gift.client.LoginUserClient;
import org.wanghong.gift.domain.LoginUser;
import org.wanghong.gift.domain.User;
import org.wanghong.gift.dto.PhoneRegisterDto;
import org.wanghong.gift.dto.RegisterPhoneValidateDto;
import org.wanghong.gift.exception.asserts.Assert;
import org.wanghong.gift.exception.statuCode.StatusCode;
import org.wanghong.gift.mapper.UserMapper;
import org.wanghong.gift.service.IUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.wanghong.gift.util.AjaxResult;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author wanghong
 * @since 2022-10-12
 */

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    //注册接口注入
    @Autowired
    private SmsValidateCodeClient smsValidateCodeClient;
    //登录接口注入
    @Autowired
    private LoginUserClient loginUserClient;

    @Override
    @GlobalTransactional
    public AjaxResult registerByPhone(PhoneRegisterDto phoneRegisterDto) {
        //接收前端传过来的数据
        String phone = phoneRegisterDto.getPhone();
        String password = phoneRegisterDto.getPassword();
        //jar303校验,校验用户是否存在
        List<User> users = userMapper.selectList(new EntityWrapper<User>().eq("phone", phone));
        Assert.assertListIsEmpty(users, StatusCode.CODE_400_USER_EXSIT);
        //验证码校验
        RegisterPhoneValidateDto dto = new RegisterPhoneValidateDto();
        dto.setPhone(phone);
        dto.setValidateCode(phoneRegisterDto.getValidateCode());
        AjaxResult ajaxResult = smsValidateCodeClient
                .validateRegisterCode(dto);
        if (!ajaxResult.isSuccess())
            return ajaxResult;
      //保存到loginuser表
        LoginUser loginUser = new LoginUser();//通过loginUser来设置参数
        loginUser.setUsername(UUID.randomUUID().toString());//通过一个uuid来为用户名
        loginUser.setPassword(password);
        loginUser.setTel(phone);
        loginUser.setType(1);
        AjaxResult result = loginUserClient.addOrUpdate(loginUser);
        if (!result.isSuccess())
            return result;
        //返回一个loginuser_id
        Long loginUserId = Long.valueOf(result.getResultObj().toString());
        //保存到user表，参数设置
        User user = new User();
        user.setCreateTime(new Date());
        user.setPhone(phone);
        user.setPassword(password);
        user.setSalt(null);
        user.setBitState(1L);
        user.setSecLevel(1);
        user.setLoginId(loginUserId);
        user.setUsername(loginUser.getUsername());
        userMapper.insert(user);
        //int i=1/0;
        return AjaxResult.me();
    }
}
